import { sendError } from "h3"
import {createUser} from "~/server/db/users.js";

export  default defineEventHandler(async (event)=>{
    const body = await readBody(event);
    const { username, password,email,repeatPassword,name } = body
    if (!username || !email || !password || !repeatPassword || !name) {
        return sendError(event, createError({ statusCode: 400, statusMessage: 'Invalid params' }))
    }

    if (password !== repeatPassword) {
        return sendError(event, createError({ statusCode: 400, statusMessage: 'Passwords do not match' }))
    }
    const userData = {
        username,
        email,
        password,
        name,
        profileImage: 'https://picsum.photos/200/200'
    }
    // 创建用户到数据库
    const user = await createUser(userData)

    return {
        body:user
    }
})